<script setup lang="ts">
import { LoginApi } from '@/api/login';
import { useUserStore } from '@/stores/user';
import type { loginParams } from '@/types/login';
import { ElMessage, type FormInstance } from 'element-plus';
import { ref } from 'vue'
import { useRouter } from 'vue-router';
const loginForm=ref<loginParams>({
  username:'demo',
  password:'Hmzs%001'
})
const loginRules=ref({
  username:[
    {required:true,message:'请输入账号',trigger:'blur'},
  ],
  password:[
    {required:true,message:'请输入密码',trigger:'blur'},
  ]
})
const loginRef  =ref<FormInstance>()
const store=useUserStore()
const router=useRouter()
const getLogin=async ()=>{
  const res= await LoginApi(loginForm.value)
  console.log('res',res);
 if(res.code===10000){
   store.setStoreToken(res.data.token)
   router.push('/')
   ElMessage.success('登录成功')
 }
}
const OnLogin=()=>{
  loginRef.value?.validate(valid => {
    try {
      if (valid) {
      getLogin()
    }
    } catch {
    }
  })
}

</script>

<template>
  <div class="login-page">
    <div class="bg"></div>
    <div class="login-form">
      <div class="title">智慧园区-登录</div>
     <el-form :model="loginForm" ref="loginRef" :rules="loginRules">
      <el-form-item label="账号" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
        <el-form-item>
          <el-checkbox>记住我</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login_btn" @click="OnLogin">登录</el-button>
        </el-form-item>
     </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-page {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url("@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}

.login-form {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }
}

.login_btn {
  width: 100%;
}
</style>

